package cn.wang.service.cargo.Impl;

import cn.wang.dao.BaseDao;
import cn.wang.entity.cargo.Contract;
import cn.wang.entity.cargo.ContractProduct;
import cn.wang.entity.cargo.Factory;
import cn.wang.service.cargo.ContractProductService;
import cn.wang.utils.Pagination;
import cn.wang.utils.UtilFuns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by john on 2017/11/1.
 */
@Service("contractProductProductServiceImpl")
@Transactional
class ContractProducterviceImpl implements ContractProductService {
    @Autowired
    private BaseDao<ContractProduct, String> cpDao;
    @Autowired
    BaseDao<Factory, String> factoryBaseDao;
    @Autowired
    private BaseDao<Contract, String> contractBaseDao;

    @Override
    public Pagination findPage(int pageNo, int pageSize) {


        return cpDao.pageByHql("from ContractProduct", pageNo, pageSize);
    }

    @Override
    public List<Factory> findAll() {
        String ctype = "附件";
        return factoryBaseDao.getListByHQL("from Factory where ctype=? and state=?", ctype, 1);
    }

    @Override
    public void save(ContractProduct contractProduct) {
        //设置总金额
        double subCunber = contractProduct.getPrice() * contractProduct.getCnumber();
        contractProduct.setAmount(subCunber);
        Contract contract = contractBaseDao.get(Contract.class, contractProduct.getContract().getId());
        contract.setTotalAmount(contract.getTotalAmount() + subCunber);
        //保存合同总金额
        contractBaseDao.update(contract);
        cpDao.save(contractProduct);
    }


    @Override
    public void update(ContractProduct model) {
        //TODO 不能为负数
        //设置货物金额 数量*单价
        double subCunber = model.getPrice() * model.getCnumber();
        model.setAmount(subCunber);
        //设置合同总金额 老数据+货物总金额
        //查询出合同金额数据数据
        Contract contract = contractBaseDao.get(Contract.class, model.getContract().getId());
        contract.setTotalAmount(contract.getTotalAmount() + subCunber);
        //保存合同总金额
        contractBaseDao.update(contract);
        cpDao.update(model);
    }

    @Override
    public ContractProduct findById(String id) {
        return cpDao.get(ContractProduct.class, id);
    }

    /**
     * 删除时设置合同总金额 老数据减去被删产品金额
     *
     * @param ids
     */
    @Override
    public void delete(String[] ids, String id) {
        //业务层放到service 保证在同一事务
        //查询出老金额数据
        Double subContract = 0.0;
        for (String cpid : ids) {
            subContract += cpDao.get(ContractProduct.class, cpid).getAmount();
            cpDao.deleteById(ContractProduct.class, cpid);
        }

        Contract contract = contractBaseDao.get(Contract.class, id);
        contract.setTotalAmount(contract.getTotalAmount() - subContract);
        contractBaseDao.update(contract);
    }

    @Override
    public Pagination findPage1(int pageNo, int pageSize, String id) {
        return contractBaseDao.pageByHql("from ContractProduct cp where cp.contract.id='" + id + "'", pageNo, pageSize);

    }

    @Override
    public List<ContractProduct> findByshipTime(String inputDate) {
        return cpDao.getListByHQL("from ContractProduct cp where to_char(cp.contract.shipTime,'yyyy-MM')=?", inputDate);
    }

    @Override
    public List<ContractProduct> findByContractids(String[] contractids) {
        //contractIds数组转成  string  '','',''
        String ids = UtilFuns.joinInStr(contractids);
        return cpDao.getListByHQL("from ContractProduct cp where cp.contract.id in(" + ids + ")");

    }
}
